package com.frxuexi.lib_commom

import com.google.gson.Gson
import com.zyj.retrofit.adapter.FlowCallAdapterFactory
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit

object RetrofitManager {
    @JvmField
    var retrofit:Retrofit?=null

    fun  getRetrofit():Retrofit {
        if (retrofit == null) {
            synchronized(RetrofitManager::class.java) {
                if (retrofit == null) {
                    val client = OkHttpClient.Builder()
                        .writeTimeout(10, TimeUnit.SECONDS)
                        .readTimeout(10, TimeUnit.SECONDS)
                        .connectTimeout(10, TimeUnit.SECONDS)
//                        .addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
                        .addInterceptor {
//                            val token = MMKV.defaultMMKV().decodeString("token", "")
//                            val token="eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ7XCJjcmVhdGVUaW1lXCI6MTcxNzM5MzUwMjAwMCxcImZsYWdcIjoxLFwiaWRcIjo0OTYsXCJwYXNzd29yZFwiOlwiXCIsXCJwaG9uZVwiOlwiMTU2NjAxMTIzMDZcIixcInVwZGF0ZVRpbWVcIjoxNzE3MzkzNTAyMDAwLFwidXNlcm5hbWVcIjpcImNoZW5cIn0ifQ.1S4ns6X4AlZdGqvqn9Kbs3hC9LNoEgruheYTwryF4Oo"
                            val request =
                                it.request().newBuilder().addHeader("sn-token", "").build()
                            return@addInterceptor it.proceed(request)
                        }
                        .build()

                    retrofit = Retrofit.Builder()
                        .baseUrl("http://10.161.9.80:7014/")
                        .client(client)
                        .addConverterFactory(GsonConverterFactory.create())
//                        .addCallAdapterFactory(FlowCallAdapterFactory.create())
                        .build()

                }
            }
        }
        return retrofit!!
    }
}